﻿$(document).on("pageInit", "#time-cource-list", function (e, id, page) {
    loadList();

    function loadList() {
        $(page).find("#list").empty();
        $.getJSON("/TimeTable/GetCourceList4Teacher/" + $(page).find('[name="time-table-id"]').val(), {}, function (response) {
            var week = {
                1: "一",
                2: "二",
                3: "三",
                4: "四",
                5: "五",
                6: "六",
                7: "日"
            };
            $.each(response, function (index, item) {
                var frame = $('<li class="cource-item" data-project-id="' + item.Id + '">\
                    <div class="item-content">\
                        <div class="item-inner">\
                            <div class="item-title-row">\
                                <div class="item-title">' + item.CourceName + '</div>\
                                <div class="item-after">每周' + week[item.WeekDay] + " " + item.StartTime + '-' + item.StopTime + '</div>\
                            </div>\
                            <div class="item-subtitle">任课老师：' + item.TeacherName + '</div>\
                            <div class="item-subtitle">地址：' + item.Address + '</div>\
                            <div class="item-text">备注：' + (item.Remark || '') + '</div>\
                        </div>\
                    </div>\
                </li>');
                $(page).find("#list").append(frame);
            });
        });

        $.getJSON('/timetable/GetTimeTableInfo/' + $(page).find('[name="time-table-id"]').val(), function (timetableInfo) {
            console.log(timetableInfo);
            $(page).find(".title").html(timetableInfo.TableName);
        });
    }

    $(page).find("#menu-btn").on("click", function () {
        var buttons1 = [
        {
            text: '请选择',
            label: true
        },
        {
            text: '关联课程',
            bold: true,
            onClick: function () {
                $.router.load('/timetable/createcource/' + $(page).find('[name="time-table-id"]').val(), true);
            }
        },
        {
            text: '修改课表名称',
            bold: true,
            onClick: function () {
                $.router.load('/timetable/Edit/' + $(page).find('[name="time-table-id"]').val(), true);
            }
        },
        {
            text: '删除课表',
            bold: true,
            onClick: function () {
                $.confirm("确定取消？", function () {
                    $.post("/timetable/Delete?timeTableId=" + $(page).find('[name="time-table-id"]').val(), function () {
                        history.back();
                    });
                }, function () {

                });
                //$.router.load('/timetable/Delete/' + $(page).find('[name="time-table-id"]').val(), true);
            }
        }];

        var buttons2 = [
          {
              text: '取消',
              bg: 'danger'
          }
        ];
        var groups = [buttons1, buttons2];
        $.actions(groups);
    });

    $(page).on("click", ".cource-item", function (evt) {
        var buttons1 = [
        {
            text: '请选择',
            label: true
        },
        {
            text: '取消课程',
            bold: true,
            onClick: function () {
                $.confirm("确定取消？", function () {
                    $.post("/timetable/CancelProject?projectId=" + evt.currentTarget.dataset.projectId, function () {
                        loadList();
                    });
                }, function () {

                });
            }
        }];

        var buttons2 = [
          {
              text: '取消',
              bg: 'danger'
          }
        ];
        var groups = [buttons1, buttons2];
        $.actions(groups);
    });
});